package cn.kduck.module.menu.web;

import cn.kduck.core.service.Page;
import cn.kduck.core.utils.TreeNodeUtils;
import cn.kduck.core.utils.TreeNodeUtils.Node;
import cn.kduck.core.web.annotation.ModelOperate;
import cn.kduck.core.web.annotation.ModelResource;
import cn.kduck.core.web.json.JsonObject;
import cn.kduck.core.web.json.JsonPageObject;
import cn.kduck.module.menu.service.Menu;
import cn.kduck.module.menu.service.MenuAuthorize;
import cn.kduck.module.menu.service.MenuService;
import cn.kduck.security.principal.AuthUser;
import cn.kduck.security.principal.AuthUserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@ModelResource("菜单管理")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/add")
    @ModelOperate(name="新增菜单")
    public JsonObject addMenu(Menu menu){
        menuService.addMenu(menu);
        return JsonObject.SUCCESS;
    }

    @DeleteMapping("/delete")
    @ModelOperate(name="删除菜单")
    public JsonObject  deleteMenu(@RequestParam("ids") String[] ids){
        menuService.deleteMenu(ids);
        return JsonObject.SUCCESS;
    }

    @PutMapping("/update")
    @ModelOperate(name="更新菜单")
    public JsonObject  updateMenu(Menu menu){
        menuService.updateMenu(menu);
        return JsonObject.SUCCESS;
    }

    @GetMapping("/get")
    @ModelOperate(name="查看菜单详情")
    public JsonObject  getMenu(@RequestParam("menuId") String menuId){
        Menu menu = menuService.getMenu(menuId);
        return new JsonObject(menu);
    }

    @GetMapping("/tree")
    @ModelOperate(name="查询菜单树")
    public JsonObject  treeMenu(){
        List<Menu> menus = menuService.listAllMenu();
        List<Node<Menu>> nodes = TreeNodeUtils.formatTreeNode(menus, Menu::getMenuId, Menu::getMenuName, Menu::getParentId);
        return new JsonObject(nodes);
    }

    @GetMapping("/list")
    @ModelOperate(name="查询菜单")
    public JsonObject  listMenu(@RequestParam("parentId") String parentId, @RequestParam Map<String, Object> paramMap, Page page){
        List<Menu> menus = menuService.listMenuByParentId(parentId,paramMap,page);
        return new JsonPageObject(page,menus);
    }

    @PostMapping("/authorize/save")
    @ModelOperate(name="新增菜单授权")
    public JsonObject addMenuAuthorize(@RequestParam("menuId") String menuId,@RequestParam("operateType")Integer operateType,@RequestParam("operateObject") String[] operateObject){
        menuService.saveMenuAuthorize(menuId,operateType,operateObject);
        return JsonObject.SUCCESS;
    }

    @PostMapping("/public")
    @ModelOperate(name="设置菜单为公共菜单")
    public JsonObject publicMenu(@RequestParam("menuId") String menuId){
        menuService.publicMenu(menuId);
        return JsonObject.SUCCESS;
    }

    @GetMapping("/authorize/list")
    public JsonObject listMenuAuthorize(@RequestParam("menuId") String menuId,@RequestParam("operateType") Integer operateType){
        List<MenuAuthorize> menuAuthorizeList = menuService.listMenuAuthorize(menuId,operateType);
        return new JsonObject(menuAuthorizeList);
    }

    @GetMapping("/user/list")
    public JsonObject listUserMenu(){
        AuthUser authUser = AuthUserHolder.getAuthUser();
        List<Menu> userMenuList = menuService.listMenuByUser(authUser.getUserId());
        List<Node<Menu>> nodes = TreeNodeUtils.formatTreeNode(userMenuList, Menu::getMenuId, Menu::getMenuName, Menu::getParentId);
        return new JsonObject(nodes);
    }
}
